package com.hundsun.cprs.yyt.service.scheduler;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hundsun.cprs.yyt.common.enums.EnumAreaCode;
import com.hundsun.cprs.yyt.service.goods.domain.GoodsSpecs;
import com.hundsun.cprs.yyt.service.goods.domain.GoodsWithPlate;
import com.hundsun.cprs.yyt.service.goods.service.GoodsSkuService;
import com.hundsun.cprs.yyt.service.goods.service.GoodsWithPlateService;
import com.hundsun.cprs.yyt.service.plate.domain.Plate;
import com.hundsun.cprs.yyt.service.plate.service.PlateService;


@Service
public class DaliyFrozenStockQuartzService implements Serializable {
    
    private static final long serialVersionUID = 257954232187491434L;
    
    protected static Log logger = LogFactory.getLog(DaliyFrozenStockQuartzService.class);

    @Autowired
    private GoodsWithPlateService goodsWithPlateService;
    
    @Autowired
    private PlateService plateService;
    
    @Autowired
    private GoodsSkuService goodsSkuService;
    
    public void resetFrozenStock(){
        
        logger.info("开始重置新人专场的每日优先量");
        
        Plate newPlate = plateService.getNewPlate();
        if (newPlate == null) {
            return;
        }
        
        GoodsWithPlate query = new GoodsWithPlate();
        query.setAreaCode(EnumAreaCode.RETAIL_AREA.getCode());
        query.setPlateId(newPlate.getId().intValue());
        List<GoodsWithPlate> goodsPlateList = goodsWithPlateService.selectGoodsWithPlate(query);
        if (goodsPlateList == null) {
            return;
        }
        
        for (GoodsWithPlate item : goodsPlateList) {
            if (item.getGoodsStockDaily() != null && item.getGoodsStockDaily() > 0) {
                GoodsSpecs sku = goodsSkuService.selectDistinctSku(EnumAreaCode.RETAIL_AREA.getCode(), item.getGoodsId(), newPlate.getPlateCode()); 
                if (sku != null && sku.getParent() != null) {
                    sku.getParent().setFrozenStock(item.getGoodsStockDaily().intValue());
                    goodsSkuService.updateFrozenStock(sku.getParent());
                    
                    logger.info("重置SKU[" + sku.getParent().getId() + "]完成");
                }
            }
        }
    }
    
}